<template>
  <Card dis-hover :bordered="false">
    <Form inline :label-width="0" @submit.native.prevent>
      <Row>
        <Col span="22">
          <FormItem>
            <Input
              placeholder="请输入姓名、手机号"
              v-model="params.keywords"
              @on-clear="searchData"
              @on-enter="searchData"
              clearable
            />
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="请选择状态"
              v-model="params.status"
              @on-clear="searchData"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in statusList"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
        </Col>
      </Row>
    </Form>
    <Table
      :columns="columns"
      :data="list"
      :loading="loading"
      @on-sort-change="sortChange"
      :max-height="tableHeigth"
    >
      <template #status="{ row }">
        <Tag
          type="border"
          :color="
            row.status == '2'
              ? 'error'
              : row.status == '1'
              ? 'success'
              : 'primary'
          "
          >{{ row.status_name }}</Tag
        >
      </template>
      <template #action="{ row }">
        <a
          @click="handleDetai(row)"
          v-hasC="{
            name: 'v1-buss-spoker-account',
            flag: 'v1/buss/spoker/account',
          }"
          >提现信息</a
        >
        <Divider
          type="vertical"
          v-hasC="{
            name: 'v1-buss-spoker-examine',
            flag: 'v1/buss/spoker/examine',
          }"
        />
        <a
          @click="handleEdit(row)"
          v-hasC="{
            name: 'v1-buss-spoker-examine',
            flag: 'v1/buss/spoker/examine',
          }"
          v-show="row.status == '0' || row.status == '2'"
          >审核</a
        >
        <a
          @click="handleEdit(row)"
          v-hasC="{
            name: 'v1-buss-spoker-examine',
            flag: 'v1/buss/spoker/examine',
          }"
          v-show="row.status == '1'"
          disabled
          >审核</a
        >
      </template>
    </Table>
    <Page
      class="ivu-mt-16"
      :total="total"
      :modelValue="params.current"
      :page-size="params.size"
      :page-size-opts="pageOpts"
      show-total
      show-sizer
      @on-change="changePage"
      @on-page-size-change="changepageSize"
    />
    <Modal
      :mask-closable="false"
      v-model="addModal"
      title="审核"
      :footer-hide="true"
      :draggable="true"
      :width="400"
    >
      <Form ref="create" :model="formInfo">
        <FormItem prop="status">
          <RadioGroup v-model="formInfo.status" class="radio">
            <Radio :label="1">通过</Radio>
            <Radio :label="2">拒绝</Radio>
          </RadioGroup>
        </FormItem>
      </Form>
      <div slot="footer" style="text-align: right">
        <Button type="default" @click="addModal = false">取消</Button>
        <Button
          type="primary"
          @click="ok"
          :loading="btnLoading"
          style="margin-left: 10px"
          >确定</Button
        >
      </div>
    </Modal>
    <detail
      :detailModal="detailModal"
      @closeDetailModal="closeDetailModal"
      :spokerId="spokerId"
    />
  </Card>
</template>
<script>
import { spokerList, spokerExamine } from "@api/spokensman.js";
import detail from "./detail";
export default {
  name: "spokensman",
  components: { detail },
  data() {
    return {
      tableHeigth: document.body.clientHeight - 270,
      addModal: false,
      detailModal: false,
      spokerId: "",
      params: {
        keywords: "",
        status: "",
        current: 1,
        size: 10,
        order_column: "",
        order_sort: "",
      },
      statusList: [],
      total: 0,
      pageOpts: [10, 20, 30, 40],
      columns: [
        {
          key: "name",
          title: "姓名",
        },
        {
          key: "mobile",
          title: "电话",
        },
        {
          key: "referrer_name",
          title: "推荐人",
        },
        {
          slot: "status",
          title: "状态",
        },
        {
          key: "all_num",
          title: "新增",
        },
        {
          key: "in_hospital_num",
          title: "到院",
        },
        {
          key: "sale_money",
          title: "销售额",
        },
        {
          key: "created_at_str",
          title: "提交日期",
        },
        {
          slot: "action",
          title: "操作",
          width: 140,
        },
      ],
      list: [],
      loading: false,
      btnLoading: false,
      formInfo: {
        status: 1,
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    //切换page
    changePage(index) {
      this.params.current = index;
      this.getData();
    },
    changepageSize(val) {
      this.params.size = val;
      this.getData();
    },
    sortChange(e) {
      this.params.order_column = e.key.replace(/_str$/, "");
      this.params.order_sort = e.order;
      this.getData();
    },
    handleDetai(info) {
      this.spokerId = info.id;
      this.detailModal = true;
    },
    closeDetailModal() {
      this.detailModal = false;
    },
    handleEdit(info) {
      this.addModal = true;
      this.formInfo = {
        id: info.id,
        status: +info.status ? +info.status : 1,
      };
    },
    getData() {
      this.loading = true;
      const params = {
        keywords: this.params.keywords,
        status: this.params.status,
        order_column: this.params.order_column,
        order_sort: this.params.order_sort,
        pageSize: this.params.size,
        page: this.params.current,
      };
      spokerList(params)
        .then((res) => {
          this.statusList = res.condition;
          this.list = res.list.list;
          this.total = res.list.total;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    searchData() {
      this.params.current = 1;
      this.getData();
    },
    editInfo() {
      this.btnLoading = true;
      const params = {
        id: this.formInfo.id,
        status: this.formInfo.status,
      };
      spokerExamine(params)
        .then((res) => {
          this.$Message.success("编辑成功");
          this.addModal = false;
          this.btnLoading = false;
          this.getData();
        })
        .catch(() => {
          this.btnLoading = false;
        });
    },
    ok() {
      this.$refs.create.validate((valid) => {
        if (valid) {
          this.editInfo();
        } else {
          this.addModal = true;
          return;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.radio {
  display: flex;
  justify-content: space-around;
}
</style>
